<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryRef" :inline="true" :model="queryParams" label-width="60px">
      <el-form-item label="手续费封顶值" prop="capValue" label-width="100px">
        <el-input style="width: 230px" v-model="queryParams.capValue" clearable placeholder="请输入手续费封顶值" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="报警值" prop="prewarningValue">
        <el-input style="width: 230px" v-model="queryParams.prewarningValue" clearable placeholder="请输入报警值" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="手续费" prop="serviceCharge">
        <el-input style="width: 230px" v-model="queryParams.serviceCharge" clearable placeholder="请输入手续费" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="已结手续费" prop="closedServiceCharge" label-width="90px">
        <el-input style="width: 230px" v-model="queryParams.closedServiceCharge" clearable placeholder="请输入已结手续费" @keyup.enter="handleQuery" />
      </el-form-item>

      <el-form-item>
        <el-button icon="Search" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermi="['agent:agent:add']" icon="Plus" plain type="primary" @click="handleAdd">新增 </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermi="['agent:agent:edit']" :disabled="single" icon="Edit" plain type="success" @click="handleUpdate">修改 </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermi="['agent:agent:remove']" :disabled="multiple" icon="Delete" plain type="danger" @click="handleDelete">删除 </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermi="['agent:agent:export']" icon="Download" plain type="warning" @click="handleExport">导出 </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="agentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="" align="center" prop="id" />
      <el-table-column label="合伙人名称" align="center" prop="agentName" width="150" />
      <el-table-column label="合伙人状态" align="center" prop="agentState" width="150">
        <template #default="scope">
          <dict-tag :options="agent_state" :value="scope.row.agentState" />
        </template>
      </el-table-column>
      <el-table-column label="是否可以增加" align="center" prop="isSon" width="150">
        <template #default="scope">
          <dict-tag :options="is_son" :value="scope.row.isSon" />
        </template>
      </el-table-column>
      <!--      <el-table-column label="代理商激活状态" align="center" prop="activatedState" width="150"/>-->
      <el-table-column label="手续费封顶值" align="center" prop="capValue" width="150" />
      <el-table-column label="报警值" align="center" prop="prewarningValue" width="150" />
      <el-table-column label="手续费" align="center" prop="serviceCharge" width="150" />
      <el-table-column label="已结手续费" align="center" prop="closedServiceCharge" width="150" />
      <!--      <el-table-column label="手续费符号位" align="center" prop="serviceChargeSymbol" width="150"/>-->
      <el-table-column label="合伙人手机号" align="center" prop="agentPhone" width="150" />
      <el-table-column label="身份证正面" align="center" prop="identityFront" width="150">
        <template #default="scope">
          <image-preview :src="scope.row.identityFront" :width="50" :height="50" v-if="scope.row.identityFront" />
          <div v-else> 暂无图片 </div>
        </template>
      </el-table-column>
      <el-table-column label="身份证反面" align="center" prop="identityBack" width="150">
        <template #default="scope">
          <image-preview :src="scope.row.identityBack" :width="50" :height="50" v-if="scope.row.identityBack" />
          <div v-else> 暂无图片 </div>
        </template>
      </el-table-column>
      <el-table-column label="合伙人营业执照" align="center" prop="agentBusiness" width="150">
        <template #default="scope">
          <image-preview :src="scope.row.agentBusiness" :width="50" :height="50" v-if="scope.row.agentBusiness" />
          <div v-else> 暂无图片 </div>
        </template>
      </el-table-column>
      <el-table-column label="合伙人签署协议" align="center" prop="agentAgreement" width="150">
        <template #default="scope">
          <image-preview :src="scope.row.agentAgreement" :width="50" :height="50" v-if="scope.row.agentAgreement" />
          <div v-else> 暂无图片 </div>
        </template>
      </el-table-column>
      <el-table-column label="邮件" align="center" prop="emil" />
      <el-table-column label="贷记卡费率" align="center" prop="cardpayFeeRate" width="150" />
      <el-table-column label="借记卡费率" align="center" prop="cardpayDebitRate" width="150" />
      <el-table-column label="封顶值" align="center" prop="cardpayDebitCap" width="150" />
      <el-table-column label="银联扫码优惠费率" align="center" prop="preferstageFeerate" width="150" />
      <el-table-column label="微信费率" align="center" prop="wxpayFeerate" width="150" />
      <el-table-column label="支付宝费率" align="center" prop="alipayFeerate" width="150" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="300">
        <template #default="scope">
          <el-button type="text" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['agent:agent:edit']">修改 </el-button>
          <el-button type="text" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['agent:agent:remove']">删除 </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" v-model:limit="queryParams.pageSize" v-model:page="queryParams.pageNum" :total="total" @pagination="getList" />
    <agent-add :open="open" :datas="form" @update="cancel" />
  </div>
</template>

<script name="Agent" setup>
  import { delAgent, getAgent, listAgent } from '@/api/agent/agent';
  import { getCurrentInstance, reactive, ref, toRefs } from 'vue';
  import AgentAdd from './add.vue';

  const { proxy } = getCurrentInstance();

  const { commercial_tenant_state, is_son, agent_state } = proxy.useDict('commercial_tenant_state', 'is_son', 'agent_state');
  const agentList = ref([]);
  const open = ref(false);
  const loading = ref(true);
  const showSearch = ref(true);
  const ids = ref([]);
  const single = ref(true);
  const multiple = ref(true);
  const total = ref(0);
  const title = ref('');

  const data = reactive({
    form: {},
    queryParams: {
      pageNum: 1,
      pageSize: 10,
      parentIds: null,
      capValue: null,
      prewarningValue: null,
      serviceCharge: null,
      closedServiceCharge: null,
      serviceChargeSymbol: null,
    },
    rules: {},
  });

  const { queryParams, form, rules } = toRefs(data);

  /** 查询代理商列表 */
  function getList() {
    loading.value = true;
    listAgent(queryParams.value).then((response) => {
      agentList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    });
  }

  // 取消按钮
  function cancel() {
    open.value = false;
    reset();
  }

  // 表单重置
  function reset() {
    form.value = {
      id: null,
      parentIds: null,
      capValue: null,
      prewarningValue: null,
      serviceCharge: null,
      closedServiceCharge: null,
      serviceChargeSymbol: null,
      createBy: null,
      createTime: null,
      updateBy: null,
      updateTime: null,
      delFlag: null,
    };
    proxy.resetForm('agentRef');
  }

  /** 搜索按钮操作 */
  function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
  }

  /** 重置按钮操作 */
  function resetQuery() {
    proxy.resetForm('queryRef');
    handleQuery();
  }

  // 多选框选中数据
  function handleSelectionChange(selection) {
    ids.value = selection.map((item) => item.id);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
  }

  /** 新增按钮操作 */
  function handleAdd() {
    reset();
    open.value = true;
    title.value = '添加合伙人';
  }

  /** 修改按钮操作 */
  function handleUpdate(row) {
    reset();
    const _id = row.id || ids.value;
    getAgent(_id).then((response) => {
      form.value = response.data;
      open.value = true;
      title.value = '修改合伙人';
    });
  }

  /** 删除按钮操作 */
  function handleDelete(row) {
    const _ids = row.id || ids.value;
    proxy.$modal
      .confirm('是否确认删除合伙人编号为"' + _ids + '"的数据项？')
      .then(function () {
        return delAgent(_ids);
      })
      .then(() => {
        getList();
        proxy.$modal.msgSuccess('删除成功');
      })
      .catch(() => {});
  }

  /** 导出按钮操作 */
  function handleExport() {
    proxy.download(
      'agent/agent/export',
      {
        ...queryParams.value,
      },
      `agent_${new Date().getTime()}.xlsx`,
    );
  }

  getList();
</script>
